clear
set more off
capture log close

use "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 6 Regionalism\Data\base panel mexico 2006.dta", clear 

gen respond1=1 if dia~=.
gen respond2=1 if dia_2~=.
gen respond3=1 if dia_3~=.
recode respond* (.=0)

*I am downweighting the states that were oversampled, including df
gen pweight=1
replace pweight=160/	(160+500) if estado==9
replace pweight=80/(80+140) if estado==7
replace pweight=100/(80+100) if estado==14
replace pweight=60/(80+60) if estado==20	
svyset [pweight=pweight], psu(estado)

*----------------------------------

*IVs
	*DISCUSSANT IVS
		*Count discussants
egen disctotal2=rsum( p66a_2 p66b_2 p66c_2)
replace disctotal2=. if respond2~=1
replace disctotal2=. if tipomu_2==4

egen disctotal3=rsum(p48a_3 p48b_3 p48c_3)
replace disctotal3=. if respond3~=1
replace disctotal3=. if tipomu_3==4

recode p67a_2 p67b_2 p67c_2 (1/2=1) (3/7=0) (8/9=-1) (.=-1) , gen(relate12 relate22 relate32)
replace relate12=. if p66a_2==.
replace relate22=. if p66b_2==.
replace relate32=. if p66c_2==.

egen disctotalfam2=anycount(relate12 relate22 relate32), values(1)
replace disctotalfam2=. if respond2~=1
replace disctotalfam2=. if tipomu_2==4

egen disctotalfrnd2=anycount(relate12 relate22 relate32), values(0)
replace disctotalfrnd2=. if respond2~=1
replace disctotalfrnd2=. if tipomu_2==4

egen disctotalunspec2=anycount(relate12 relate22 relate32), values(-1)
replace disctotalunspec2=. if respond2~=1
replace disctotalunspec2=. if tipomu_2==4

recode p49a_3 p49b_3 p49c_3 (1/2=1) (3/7=0) (8/9=-1) (.=-1) , gen(relate13 relate23 relate33)
replace relate13=. if p49a_3==.
replace relate23=. if p49b_3==.
replace relate33=. if p49c_3==.

egen disctotalfam3=anycount(relate13 relate23 relate33), values(1)
replace disctotalfam3=. if respond3~=1
replace disctotalfam3=. if tipomu_3==4

egen disctotalfrnd3=anycount(relate13 relate23 relate33), values(0)
replace disctotalfrnd3=. if respond3~=1
replace disctotalfrnd3=. if tipomu_3==4

egen disctotalunspec3=anycount(relate13 relate23 relate33), values(-1)
replace disctotalunspec3=. if respond3~=1
replace disctotalunspec3=. if tipomu_3==4

*Stuff for Figure 2.3
svy: prop disctotal2
svy: prop disctotal3

svy: mean disctotal2
svy: mean disctotalfam2 
svy: mean disctotalfrnd2
svy: mean disctotalunspec2

svy: mean disctotal3
svy: mean disctotalfam3
svy: mean disctotalfrnd3
svy: mean disctotalunspec3

svy: tab p50a_3  
svy: tab p50b_3 
svy: tab p50c_3 

recode p3 p3_2 p47_3 (1=4) (2=3) (3=2) (4=1) (5=0) (6=.)
label define freq 0 Never 1 Rarely 2 "Few times per month" 3 "Few times per week" 4 "Daily"
label values p3 p3_2 p47_3 freq

* uncomment this to get means, but notice this will mess up stuff on awareness below.
* it's easier if you use ordinal for histograms
*recode p3 p3_2 p47_3 (0=0) (1=1) (2=4) (3=15) (4=30)

svy: mean p3 
svy: mean p3_2 
svy: mean p47_3

svy: mean p3 if dia~=. & dia_2~=. & dia_3~=.
svy: mean p3_2 if dia~=. & dia_2~=. & dia_3~=.
svy: mean p47_3 if dia~=. & dia_2~=. & dia_3~=.
	
replace p3=p3-.25
replace p47_3 =p47_3 + .25

*Figure 2.4A
twoway (histogram p3, width(.25) discrete fraction color(black)) (histogram p3_2, width(.25) discrete fraction color(gs6)) ///
(histogram p47_3, width(.25) discrete fraction color(gs10)), ///
xlab(0 "Never" 1 "Rarely" 2 "Few times/month" 3 "Few times/week" 4 "Daily", labsize(small) ) ///
ylab(0 "0%" .1 "10%" .2 "20%" .3 "30%" .4 "40%" .5 "50%") ///
xtick(-.25 0 .25 .75 1 1.25 1.75 2 2.25 2.75 3 3.25 3.75 4 4.25) xscale(range(-.5 4.5)) ///
graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) ytitle("{bf:Percentage of Respondents}" "  ") ///
xtitle("{bf:Frequency of Political Discussion}") ///
legend(label(1 "{it:pc}: 8 months b/f election  " "({it:x̄}=6.67 days/month)") label(2 "{it:c}: 2 months b/f election  " "({it:x̄}=7.35 days/month)") label(3 "{it:e}: Just after election     " "({it:x̄}=6.54 days/month)") rows(1) region(lcolor(white)) keygap(.2) size(small) span ) 
graph export "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 2 Description\Figures\MexHistDiscuss.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR2_4A.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR2_4A.pdf", as(pdf) replace 


gen news11=0 if p14a==1
replace news11=4 if p15a==1
replace news11=3 if p15a==2
replace news11=2 if p15a==3
replace news11=1 if p15a==4

gen news21=0 if p14b==1
replace news21=4 if p15b==1
replace news21=3 if p15b==2
replace news21=2 if p15b==3
replace news21=1 if p15b==4

egen news1=rowmax(news11 news21)

gen news12=0 if p15a_2==1
replace news12=4 if p16a_2==1
replace news12=3 if p16a_2==2
replace news12=2 if p16a_2==3
replace news12=1 if p16a_2==4

gen news22=0 if p15b_2==1
replace news22=4 if p16b_2==1
replace news22=3 if p16b_2==2
replace news22=2 if p16b_2==3
replace news22=1 if p16b_2==4

egen news2=rowmax(news12 news22)

gen news13=0 if p12a_3==1
replace news13=4 if p13a_3==1
replace news13=3 if p13a_3==2
replace news13=2 if p13a_3==3
replace news13=1 if p13a_3==4

gen news23=0 if p12b_3==1
replace news23=4 if p13b_3==1
replace news23=3 if p13b_3==2
replace news23=2 if p13b_3==3
replace news23=1 if p13b_3==4

egen news3=rowmax(news13 news23)

*Party contacts
	*I ignore first wave measure of this b/c it was not coded in a comparable way
recode p34a-p34i (2=0) (1=1) (else=.)
egen partycontact1=rsum(p34a-p34i)

recode p41_2 (2=0) (1=1) (else=.), gen(partycontact2)
replace partycontact2=. if respond2==0

recode p28_3 (2=0) (1=1) (else=.), gen(partycontact3)
replace partycontact3=. if respond3==0

egen partycontact=rowmean(partycontact2 partycontact3)

*Clientelism
*Pre-Election (May) Wave
gen client_2=p42_2
replace client_2=. if client_2==3
recode client_2 (1=1) (2=0)
replace client_2=. if respond2==0

*Post-Election (July) Wave
gen client_3=p29_3
replace client_3=. if client_3==3 
recode client_3 (1=1) (2=0)
replace client_3=. if respond3==0

egen client=rowmean(client_2 client_3)

* uncomment this to get means, but notice this will mess up stuff on awareness below.
* it doesnt matter if use ordinal for histograms
*recode news* (0=0) (1=1) (2=4) (3=15) (4=30)

svy: mean news1 
svy: mean news2
svy: mean news3

svy: mean news1 if dia~=. & dia_2~=. & dia_3~=.
svy: mean news2 if dia~=. & dia_2~=. & dia_3~=.
svy: mean news3 if dia~=. & dia_2~=. & dia_3~=.

replace news1=news1-.25
replace news3 =news3 + .25

label define freq2 0 Never 1 Sometimes 2 "Once a week" 3 "Few times per week" 4 "Daily"
label values news1 news11 news21 news2 news12 news22 news13 news23 news3 freq

*Figure 2.4B
twoway (histogram news1, width(.25) discrete fraction color(black)) (histogram news2, width(.25) discrete fraction color(gs6)) ///
(histogram news3, width(.25) discrete fraction color(gs10)), ///
xlab(0 "Never" 1 "Sometimes" 2 "Once a week" 3 "Few times/week" 4 "Daily", labsize(small) ) ///
ylab(0 "0%" .1 "10%" .2 "20%" .3 "30%" .4 "40%" .5 "50%") ///
xtick(-.25 0 .25 .75 1 1.25 1.75 2 2.25 2.75 3 3.25 3.75 4 4.25) xscale(range(-.5 4.5)) ///
graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) ytitle("{bf:Percentage of Respondents}" "  ") ///
xtitle("{bf:Frequency of TV News Consumption}") ///
legend(label(1 "{it:pc}: 8 months b/f election  " "({it:x̄}=18.92 days/month)") label(2 "{it:c}: 2 months b/f election  " "({it:x̄}=17.06 days/month)") label(3 "{it:e}: Just after election     " "({it:x̄}=15.39 days/month)") rows(1) region(lcolor(white)) keygap(.2) size(small) span ) 
graph export "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 2 Description\Figures\MexHistNews.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR2_4B.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR2_4B.pdf", as(pdf) replace 


*AWARENESS Stuff for Table 2.1
recode p32a 4=1 1=0 2=0 3=0 5=0 6=0 7=0 8=0 9=0, gen(correct1)
recode p32b 2=1 1=0 3=0 4=0 5=0 6=0 7=0 8=0 9=1, gen(correct2)
recode p32c 1=1 2=0 3=0 4=0 5=0 6=0 7=0 8=1 9=0, gen(correct3)
recode p32d 3=1 1=0 2=0 4=0 5=0 6=0 7=1 8=0 9=0, gen(correct4)

svy: irt 2pl correct1-correct4 
predict aware, latent ebmeans
egen awarez=std(aware)

/*
polychoricpca correct1-correct4, score(awaretemp) nscore(1)
impute awaretemp correct1-correct4 if tipomues~=., gen(aware)
summ aware
gen awarez=(aware-r(mean))/r(Var)^.5
*/

recode p32a_2 2=1 1=0 4=0 3=0 5=0 6=0 7=0 8=0 9=0, gen(correct9)
recode p32b_2 3=1 1=0 2=0 4=0 5=0 6=0 7=0 8=0 9=1, gen(correct10)
recode p32c_2 1=1 3=0 2=0 4=0 5=0 6=0 7=0 8=0 9=1, gen(correct11)

svy: irt 2pl correct9-correct11 if respond2==1
predict aware2 if respond2, latent ebmeans 
egen aware2z=std(aware2)

/*
polychoricpca correct9-correct11, score(aware2temp) nscore(1)
impute aware2temp correct9-correct11 if respond2==1, gen(aware2)
replace aware2=. if respond2~=1
egen aware2z=std(aware2)
*/
*These quiz questions were asked in the pre-campaign and campaign panel waves. 
*can either uncomment the 2 things above or this, not both.
recode p3 p3_2 p47_3 (-.25=0) (.75=1) (1.75=4) (2.75=15) (3.75=30)
recode news1 (-.25=0) (.75=1) (1.75=4) (2.75=15) (3.75=30)

polychoric awarez news1 p3
pcorr awarez news1 p3

recode p3_2 (0=0) (1=1) (2=4) (3=15) (4=30), gen(talk2)
recode news2 (0=0) (1=1) (2=4) (3=15) (4=30), gen(news2foraware)

polychoric talk2 disctotal2
polychoric p47_3 disctotal3

gen partycontact_foraware=partycontact2*10
gen client_foraware=client_2*10
label variable p3 "Frequency of pol. discussion"
label variable talk2 "Political discussion frequency"
label variable news2foraware "TV news viewing frequency"
label variable news1 "TV news viewing frequency"
label variable disctotal2 "Egocentric network size"
label variable partycontact_foraware "Contacted by a party"
label variable client_foraware "Paid off by a party"

*Figure 2.5, Table A.1
eststo clear
eststo: svy: reg awarez p3 news1
eststo: svy: reg aware2z talk2 news2foraware partycontact_foraware client_foraware
eststo: svy: reg aware2z disctotal2 news2foraware partycontact_foraware client_foraware
*eststo: svy: reg aware2z talk2 news2foraware partycontact2 client_2
*eststo: svy: reg aware2z disctotal2 news2foraware partycontact2 client_2

cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 2 Description\Code\Output\"

esttab est1 est2 est3 using table21.rtf, label b(3) replace se star(* 0.05 ) nogap onecell title(Campaign Knowledge by Exposure to Political Intermediaries in Mexico 2006: Results from Three OLS Models)

coefplot (est1, label(Model 1 ({it:pc} wave). {it:N}=2,231) mcolor(black) ciopts(lcolor(black) recast(rcap)))  ///
(est2, label(Model 2 ({it:c} wave). {it:N}=1,632) mcolor(gs7) msymbol(S) ciopts(lcolor(gs7) recast(rcap)))  ///
(est3, label(Model 3 ({it:c} wave). {it:N}=1,653) mcolor(gs10) msymbol(t) msize(medlarge) ciopts(lcolor(gs10) recast(rcap))) , ///
drop(_cons) xline(0, lcolor(black)) rename(talk2=p3 news2foraware=news1) yline(1.5 2.5 3.5 4.5, lcolor(gs8)) ///
legend(cols(1)) graphregion(color(white)) order(p3 disctotal2 news1 partycontact_foraware client_foraware) plotregion(lstyle(yxline) lcolor(black)) ///
coeflabel(,wrap(20)) msize(small) ///
xtitle("{bf:OLS Coefficient}") ytitle("{bf:Independent Variables:}" "{bf:Political Intermediaries}") ///
title("{bf:Dependent Variable: Campaign Knowledge}", size(medsmall) color(black)) ///
text(.75 .043 ".025", color(black) size(small)) ///
text(1 .043 ".021", color(gs7) size(small)) ///
text(2.08 .167 ".169", color(gs10) size(small)) ///
text(2.75 .031 ".008", color(black) size(small)) ///
text(3 .031 ".013", color(gs7) size(small)) ///
text(3.25 .031 ".012", color(gs7) size(small)) 

coefplot (est1, label(Model 1 ({it:pc} wave). {it:N}=2,231) mcolor(black) ciopts(lcolor(black) recast(rcap)))  ///
(est2, label(Model 2 ({it:c} wave). {it:N}=1,632) mcolor(black) msymbol(S) ciopts(lcolor(black) recast(rcap)))  ///
(est3, label(Model 3 ({it:c} wave). {it:N}=1,653) mcolor(black) msymbol(t) msize(medlarge) ciopts(lcolor(black) recast(rcap))) , ///
drop(_cons) xline(0, lcolor(black)) rename(talk2=p3 news2foraware=news1) yline(1.5 2.5 3.5 4.5, lcolor(gs8)) ///
legend(cols(1)) graphregion(color(white)) order(p3 disctotal2 news1 partycontact_foraware client_foraware) plotregion(lstyle(yxline) lcolor(black)) ///
coeflabel(,wrap(20)) msize(small) ///
xtitle("{bf:OLS Coefficient}") ytitle("{bf:Independent Variables:}" "{bf:Political Intermediaries}") ///
title("{bf:Dependent Variable: Campaign Knowledge}", size(medsmall) color(black)) ///
text(.75 .043 ".025", color(black) size(small)) ///
text(1 .043 ".021", color(black) size(small)) ///
text(2.08 .167 ".169", color(black) size(small)) ///
text(2.75 .031 ".008", color(black) size(small)) ///
text(3 .031 ".013", color(black) size(small)) ///
text(3.25 .031 ".012", color(black) size(small)) 
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 2 Description\Figures\Mex2006CoefPlot.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR2_5.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR2_5.pdf", as(pdf) replace 


*STRATIFICATION STUFF 
*Media
recode news2 (0=0) (1=1) (2=4) (3=15) (4=30)
recode news3 (.25=0) (1.25=1) (2.25=4) (3.25=15) (4.25=30)
egen mediaattention=rowmean(news2 news3)

*Discussion frequency
recode p3_2 (0=0) (1=1) (2=4) (3=15) (4=30)
recode p47_3 (.25=0) (1.25=1) (2.25=4) (3.25=15) (4.25=30)
egen conversemean=rowmean(p3_2 p47_3)

*Discussants
egen disctotalmean=rowmean(disctotal2 disctotal3)

* SES
recode p59 10=., gen(eductemp)
gen educ=ln(eductemp)

recode p64a-p64h (2=0)
recode p86 7=1 1/6=0 8=. 9=., gen(home)
*polychoricpca p64a p64b p64c p64d p64e p64f p64g p64h home, score(wealthtemp1) nscore(1)
*impute wealthtemp1 p64a p64b p64c p64d p64e p64f p64g p64h home if tipomues~=., gen(wealth1)

irt 2pl p64a p64b p64c p64d p64e p64f p64g p64h home
predict wealth1, latent ebmeans 

* Age
gen age = ln(d)

* Female
recode a 2=0, gen(male)

* Ethnicity
recode b 1=3 2=2 3=1 4=. 5=., gen(race1)
recode b_2 1=3 2=2 3=1 4=. 5=., gen(race2)
recode b_3 1=3 2=2 3=1 4=. 5=., gen(race3)

egen racemean=rowmean(race1 race2 race3)
 

*AWARENESS 3 FOR AWARENESS AND SES/GENDER FIGURE. USE FIRST ROUND B/C IT HAS BEST SAMPLING PROPERTIES. 
drop correct*
recode p32a 4=1 1=0 2=0 3=0 5=0 6=0 7=0 8=0 9=0, gen(correct1)
recode p32b 2=1 1=0 3=0 4=0 5=0 6=0 7=0 8=0 9=1, gen(correct2)
recode p32c 1=1 2=0 3=0 4=0 5=0 6=0 7=0 8=1 9=0, gen(correct3)
recode p32d 3=1 1=0 2=0 4=0 5=0 6=0 7=1 8=0 9=0, gen(correct4)
recode p71 1=1 2=0 3=0 4=0, gen(correct5)
recode p72a-p72c p65a_3 p65b_3 p65c_3 (2=0)
gen correct6=p72a
gen correct7=p72b
gen correct8=p72c
gen correct12=p65a_3
gen correct13=p65b_3
gen correct14=p65c_3
egen correct_poderes=anycount(correct6 correct7 correct8), values(1)
egen correct_poderes3=anycount(correct12 correct13 correct14), values(1)
recode p69a_2 1=1 2/6=0 .=., gen(correct9)
replace correct9=. if respond2~=1
recode p69b_2 2=1 1=0 3/6=0 .=., gen(correct10)
replace correct10=. if respond2~=1
recode p69c_2 3=1 1/2=0 4/6=0 .=., gen(correct11)
replace correct11=. if respond2~=1
egen ft_know=rownonmiss(p21a p21b p21c p21d p21e p21f p21g p21h p21i p21j p21k p21l p21m)

svy: irt grm correct1 correct2 correct3 correct4 correct5 correct_poderes correct_poderes3 correct9 correct10 correct11 ft_know, difficult
predict irt_know, latent ebmeans
egen aware3z=std(irt_know)

*enter manually in other code
*Figure 8.3, Table A.23
eststo clear
eststo: svy: reg aware3z wealth1 educ male 
margin, at((p5) wealth1 educ) at((p95) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealthMX06p=r(b)
mat vwealthMX06p=r(V)
margin, at((p5) male) at((p95) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexMX06p=r(b)
mat vsexMX06p=r(V)
cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 2 Description\Code\Output\"
esttab est1 using Brazil2014strataware.rtf, label b(3) replace se star(* 0.05 ) nogap onecell title(awareness Mexico 2006)
xyz

*Start regs
egen mediaattention_std=std(mediaattention)
egen disctotalmean_std=std(disctotalmean)
egen conversemean_std=std(conversemean)
egen partycontact_std=std(partycontact)
egen client_std=std(client)

label variable wealth1 "Wealth"
label variable educ "Education level"
label variable male "Male"
label variable racemean "Race"
label variable mediaattention_std "Frequency of TV news viewing"
label variable conversemean_std "Frequency of political discussion"
label variable disctotalmean_std "Egocentric network size"
label variable partycontact_std "Contacted by party"
label variable client_std "Received payoff by party"

eststo clear
eststo: svy: reg conversemean_std wealth1 educ male racemean
margin, at((p5) wealth1 educ) at((p95) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealth1=r(b)
mat vwealth1=r(V)
margin, at((p5) male) at((p95) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsex1=r(b)
mat vsex1=r(V)
margin, at((p5) racemean) at((p95) racemean) contrast(atcontrast(r)) vce(unconditional) atmeans
mat brace1=r(b)
mat vrace1=r(V)

eststo: svy: reg disctotalmean_std wealth1 educ male racemean
margin, at((p5) wealth1 educ) at((p95) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealth2=r(b)
mat vwealth2=r(V)
margin, at((p5) male) at((p95) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsex2=r(b)
mat vsex2=r(V)
margin, at((p5) racemean) at((p95) racemean) contrast(atcontrast(r)) vce(unconditional) atmeans
mat brace2=r(b)
mat vrace2=r(V)

eststo: svy: reg mediaattention_std wealth1 educ male racemean
margin, at((p5) wealth1 educ) at((p95) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealth3=r(b)
mat vwealth3=r(V)
margin, at((p5) male) at((p95) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsex3=r(b)
mat vsex3=r(V)
margin, at((p5) racemean) at((p95) racemean) contrast(atcontrast(r)) vce(unconditional) atmeans
mat brace3=r(b)
mat vrace3=r(V)

eststo: svy: reg partycontact_std wealth1 educ male racemean
margin, at((p5) wealth1 educ) at((p95) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealth4=r(b)
mat vwealth4=r(V)
margin, at((p5) male) at((p95) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsex4=r(b)
mat vsex4=r(V)
margin, at((p5) racemean) at((p95) racemean) contrast(atcontrast(r)) vce(unconditional) atmeans
mat brace4=r(b)
mat vrace4=r(V)

eststo: svy: reg client_std wealth1 educ male racemean
margin, at((p5) wealth1 educ) at((p95) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealth5=r(b)
mat vwealth5=r(V)
margin, at((p5) male) at((p95) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsex5=r(b)
mat vsex5=r(V)
margin, at((p5) racemean) at((p95) racemean) contrast(atcontrast(r)) vce(unconditional) atmeans
mat brace5=r(b)
mat vrace5=r(V)

mat Bwealth=[bwealth5\bwealth4\bwealth3\bwealth2\bwealth1]
mat Bsex=[bsex5\bsex4\bsex3\bsex2\bsex1]
mat Brace=[brace5\brace4\brace3\brace2\brace1]

mat Vwealth=[vwealth5\vwealth4\vwealth3\vwealth2\vwealth1]
mat Vsex=[vsex5\vsex4\vsex3\vsex2\vsex1]
mat Vrace=[vrace5\vrace4\vrace3\vrace2\vrace1]

svmat Bwealth 
svmat Bsex 
svmat Brace 
svmat Vwealth 
svmat Vsex 
svmat Vrace
gen nwealth=_n+.125
gen nsex=_n
gen nrace=_n-.125
gen n=_n
gen wealthupper=Bwealth+1.9645*(Vwealth^.5)
gen wealthlower=Bwealth-1.9645*(Vwealth^.5)
gen sexupper=Bsex+1.9645*(Vsex^.5)
gen sexlower=Bsex-1.9645*(Vsex^.5)
gen raceupper=Brace+1.9645*(Vrace^.5)
gen racelower=Brace-1.9645*(Vrace^.5)

*Figure 8.1D, Table A.21.
twoway (rcap wealthupper wealthlower nwealth if n<6, color(black) horizontal ) (scatter nwealth Bwealth if n<6, mcolor(black)) ///
(rcap sexupper sexlower nsex if n<6, color(black) horizontal ) (scatter nsex Bsex if n<6, mcolor(black) msymbol(S) msize(medsmall)) ///
(rcap raceupper racelower nrace if n<6, color(black) horizontal ) (scatter nrace Brace if n<6, mcolor(black) msymbol(t) msize(medlarge)) , ///
xtitle("{bf:Model-Predicted Change in DV}" "(upon changing SES, gender, or race" "from min to max)", size(medium)) ///
graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) ytitle("{bf:Dependent Variables(DV):}" "{bf:Exposure to Political Intermediaries}", size(medium)) ///
legend(off) xscale(range(-.5 2.2)) ///
xline(0, lcolor(black)) ylab(5 "Freq. of pol. discussion" 4 "Egocentric network size"  3 "Freq. of TV news" 2 "Contacted by a party" 1 "Paid off by a party", angle(horizontal) labsize(medium)) ///
ylab(,nogrid) xlab(-.5 0 .5 1 1.5 2.0, grid) yline(1.5 2.5 3.5 4.5, lcolor(black))
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 8 Normative\Figures\Mexico 2006.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR8_1D.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR8_1D.pdf", as(pdf) replace 

*for appendix
cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 8 Normative\Output\"
esttab est1 est2 est3 est4 est5 using Mexico2006strat.rtf, label b(3) replace se star(* 0.10 ** 0.05 ) nogap onecell title(Table: Correlates of Intermediation Exposure, Mexico 2006)
xyz
